<?php

namespace App\Http\Controllers\User;

use App\Library\Mall\BoughtTrait;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\OrderShipping;
use Illuminate\Http\Request;

class TradeController extends BaseController
{
    use BoughtTrait;
    /**
     * TradeController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->assign(['menu'=>'bought']);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function bought()
    {
        $this->assign([
            'tab'=>'all',
            'order_no'=>'',
            'shop_name'=>'',
            'pay_type'=>'',
            'time_begin'=>'',
            'time_end'=>'',
            'title'=>''
        ]);

        $queryParams = [];
        $query = $this->user->boughts()->with('items')->where('buyer_deleted', 0);

        $tab = $this->request->get('tab');
        if ($tab) {
            $queryParams['tab'] = $tab;
            if ($tab == 'waitPay'){
                $query = $query->where('trade_status', 1);
            }elseif ($tab == 'waitSend'){
                $query = $query->where('trade_status', 2);
            }elseif ($tab == 'waitConfirm'){
                $query = $query->where('trade_status', 3);
            }elseif ($tab == 'waitRate'){
                $query = $query->where('trade_status', 4);
            }elseif ($tab == 'refunding') {
                $query = $query->where('trade_status', 5);
            }elseif ($tab == 'closed') {
                $query = $query->where('closed', 1);
            }
        }


        $order_no = $this->request->input('order_no');
        if ($order_no){
            $queryParams['order_no'] = $order_no;
            $query = $query->where('order_no', $order_no);
        }

        $shop_name = $this->request->input('shop_name');
        if ($shop_name) {
            $queryParams['shop_name'] = $shop_name;
            $query = $query->where('shop_name', 'LIKE', "%$shop_name%");
        }

        $pay_type = $this->request->input('pay_type');
        if ($pay_type) {
            $queryParams['pay_type'] = $pay_type;
            $query = $query->where('pay_type', $pay_type);
        }

        $title = $this->request->input('title');
        if ($title) {
            $queryParams['title'] = $title;
            $query = $query->whereHas('items', function ($newQuery) use ($title){
                return  $newQuery->where('title', 'LIKE', "%$title%");
            });
        }

        $time_begin = $this->request->input('time_begin');
        if ($time_begin) {
            $queryParams['time_begin'] = $time_begin;
            $query = $query->where('created_at', '>', strtotime($time_begin));
        }

        $time_end = $this->request->input('time_end');
        if ($time_end) {
            $queryParams['time_end'] = $time_end;
            $query = $query->where('created_at', '<', strtotime($time_end));
        }
        $this->assign($queryParams);

        $orders = $query->orderByDesc('order_id')->paginate(10);
        $this->assign([
            'orders'=>$orders,
            'pagination'=>$orders->appends($queryParams)->render()
        ]);

        return $this->view('user.trade.bought');
    }

    public function detail()
    {
        $step = 'auction';
        $order_id = intval($this->request->get('order_id'));
        $order = Order::where('buyer_uid', $this->uid)->where('order_id', $order_id)->first();
        if ($order) {
            if ($order->trade_status == 2) {
                $step = 'pay';
            } elseif ($order->trade_status == 3) {
                $step = 'send';
            } elseif ($order->trade_status == 4 && $order->buyer_rate == 0) {
                $step = 'confirm';
            } elseif ($order->trade_status == 4 && $order->buyer_rate == 1) {
                $step = 'rate';
            }

            $items = OrderItem::where('order_id', $order_id)->get();
            $shipping = OrderShipping::where('order_id', $order_id)->first();
            $this->assign(compact('step', 'order_id', 'order', 'items', 'shipping'));

            $pay_type_options = trans('mall.pay_types');
            $trade_status_items = trans('mall.buyer_trade_status_items');
            $this->assign(compact('pay_type_options', 'trade_status_items'));

            return $this->view('user.trade.detail');
        }else {

            return $this->messager()->message(trans('mall.order does not exist'))->render();
        }
    }
}
